<?php

namespace App\Controller;

class JoinController extends AppBaseController
{
    private $adLogic;
    private $taskLogic;
    private $joinLogic;
    private $userLogic;
    private $wxLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->adLogic = A('App/Ad', 'Logic');
        $this->taskLogic = A('App/Task', 'Logic');
        $this->joinLogic = A('App/Join', 'Logic');
        $this->userLogic = A('App/User', 'Logic');
        $this->wxLogic = A('App/Wx', 'Logic');
    }

    /**
     * 参与任务成员的明细页面.
     */
    public function detial()
    {
        $id = I('get.id');
        $state = I('get.state');
        $state = $state ? $state : '1';
        $this->assign('id', $id);
        $this->assign('state', $state);
        $this->display();
    }

    /**
     * 任务完成提交页面.
     */
    public function submitPage()
    {
        $taskId = I('get.id');
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $infoRes = $this->joinLogic->findJoinInfo(array(
            'task_id' => $taskId,
            'user_id' => $userId,
            'state' => array('neq', -1),
        ));

        $info = $infoRes['Result'];
        if (empty($info)) {
            $this->sysErr('您未参与此任务');
        }

        if (!empty($info['proof'])) {
            $info['proof'] = explode(',', $info['proof']);
        }

        //获取微信js配置

        $selfUrl = I('server.REQUEST_SCHEME').'://'.I('server.SERVER_NAME').__SELF__;

        $confRes = $this->wxLogic->getJsApiConf($selfUrl);

        if (static::$res->isErr($confRes)) {
            $this->sysErr($confRes['Message']);
        }
        $conf = $confRes['Result'];

        $this->assign('info', $info);
        $this->assign('conf', $conf);
        $this->assign('taskId', $taskId);
        $this->assign('userInfo', $userInfo);
        $this->display();
    }

    /**
     * 参与任务成员的列表.
     */
    public function doGetJoinTaskUserList()
    {
        $id = I('post.id');
        $state = I('post.state');
        $page = I('post.page');

        $state = $state ? $state : '1';
        $page = $page ? $page : '1';
        $size = 20;

        if (strpos($state, ',')) {
            $state = array('in', explode(',', $state));
        }

        $listRes = $this->joinLogic->getJoinUserList($page, $size, array(
            'task_id' => $id,
            'state' => $state,
        ));

        $listInfo = $listRes['Result'];
        $list = $listInfo['list'];
        $count = $listInfo['count'];

        if (empty($list)) {
            $this->ajaxReturn(static::$res->err('暂无数据'));
        }

        $countPage = ceil($count / $size);
        $hasNext = $countPage > $page ? true : false;

        $this->assign('list', $list);
        $html = $this->fetch('doGetJoinTaskUserList');
        $res = static::$res->suc('成功', array(
            'html' => $html,
            'list' => $list,
            'count' => $count,
            'hasNext' => $hasNext,
        ));
        $this->ajaxReturn($res);
    }

    /**
     * 加入任务
     */
    public function doJoinTask()
    {
        $taskId = I('post.id');
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $res = $this->joinLogic->join($taskId, $userId);
        $this->ajaxReturn($res);
    }

    /**
     * 获取未完成任务数.
     */
    public function doGetUnfinishedTaskCount()
    {
        $taskType = I('post.taskType');

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $where = array(
            'state' => 1,
            'user_id' => $userId,
            'task_type' => $taskType,
        );

        $count = $this->joinLogic->statistics($where);
        $res = static::$res->suc('成功', $count);
        $this->ajaxReturn($res);
    }

    /**
     * 取消未完成任务
     */
    public function doCancelUnfinishedJoinTask()
    {
        $taskType = I('post.taskType');
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $where = array(
            'state' => 1,
            'user_id' => $userId,
            'task_type' => $taskType,
        );

        $res = $this->joinLogic->cancelJoinTask($where);
        $this->ajaxReturn($res);
    }

    /**
     * 完成任务
     */
    public function doFinishJoinTask()
    {
        $taskId = I('post.id');
        $phone = I('post.phone');
        $doingTime = I('post.doing_time');
        $proof = I('post.proof');
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        if (!empty($proof)) {
            foreach ($proof as &$img) {
                if (false === strpos($img, '/')) {
                    $imgRes = $this->wxLogic->saveProofImg($img);
                    if (static::$res->isErr($imgRes)) {
                        $this->ajaxReturn($imgRes);
                    }
                    $img = $imgRes['Result'];
                }
            }
        }

        $res = $this->joinLogic->finishJoinTask($taskId, $userId, $phone, $doingTime, $proof);
        $this->ajaxReturn($res);
    }
}
